﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_CarbonFootprintQuestion_Transport_Motorcycles : CarbonExchange.WebControls.AuditQuestionSigleItemBase
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void rptMotorcycles_ItemDataBinding(object sender, CarbonExchange.WebControls.AmeeRepeater.ItemEventArgs e)
    {
        var ameeMotorcycleType = e.Item.FindControl("ameeMotorcycleType") as CarbonExchange.WebControls.AmeeDrillDown;
        var txtMotorcycleDistance = e.Item.FindControl("txtMotorcycleDistance") as TextBox;
        var ddlMotorcycleDistance = e.Item.FindControl("ddlMotorcycleDistance") as DropDownList;
        if (ameeMotorcycleType != null && txtMotorcycleDistance != null && ddlMotorcycleDistance != null)
        {
            ameeMotorcycleType.AddOptions(Answer.GetSetting<CarbonExchange.WebControls.AmeeDrillDown.Option[]>(string.Format("ameeMotorcycleType_{0}", e.Item.ItemIndex)));
            txtMotorcycleDistance.Text = Answer.GetSetting<string>(string.Format("txtMotorcycleDistance_{0}", e.Item.ItemIndex));
            ddlMotorcycleDistance.Text = Answer.GetSetting<string>(string.Format("ddlMotorcycleDistance_{0}", e.Item.ItemIndex));
        }
    }

    protected void ameeMotorcycle_DropDownDataBound(object sender, CarbonExchange.WebControls.AmeeDrillDown.DropDownDataBindingEventArgs e)
    {
        if (e.DataCatagory.ChoiceName == "size")
        {
            

            e.DropDown.CssClass = "bigdd";
            foreach (ListItem item in e.DropDown.Items)
            {
                if (item.Text.ToLower() == "small")
                {
                    item.Text = "Small petrol motorbike, moped or scooter (<125 cc)";
                }
                else if (item.Text.ToLower() == "medium")
                {
                    item.Text = "Medium petrol motorbike (125-500cc)";
                }
                else if (item.Text.ToLower() == "large")
                {
                    item.Text = "Large petrol motorbike (>500cc)";
                }
            }
        }
        else {
            e.DropDown.CssClass = "";
        }
    }

    protected void ameeMotorcycle_LabelDataBound(object sender, CarbonExchange.WebControls.AmeeDrillDown.LabelDataBindingEventArgs e)
    {
        if (e.DataCatagory.ChoiceName == "size")
        {
            e.Label.Text = GetText("Engine");
            //e.Label.AssociatedControlID = 
        }
    }

    public override void DataBind()
    {
        rptMotorcycles.SetNumberOfItems(Answer.GetSetting<int>("rptMotorcycles"));

        base.DataBind();
    }

    protected override bool SaveQuestion()
    {
        var completed = true;
        CarbonExchange.Bll.AmeeProfileItemFactory factory = new CarbonExchange.Bll.AmeeProfileItemFactory();
        List<CarbonExchange.Bll.AnswerItem> itemsToKeep = new List<CarbonExchange.Bll.AnswerItem>();
        Answer.SetSetting<int>("rptMotorcycles", rptMotorcycles.Items.Count());
        foreach (var item in rptMotorcycles.Items)
        {
            var ameeMotorcycleType = item.FindControl("ameeMotorcycleType") as CarbonExchange.WebControls.AmeeDrillDown;
            var txtMotorcycleDistance = item.FindControl("txtMotorcycleDistance") as TextBox;
            var ddlMotorcycleDistance = item.FindControl("ddlMotorcycleDistance") as DropDownList;
            if (ameeMotorcycleType != null && txtMotorcycleDistance != null && ddlMotorcycleDistance != null)
            {
                Answer.SetSetting(string.Format("ameeMotorcycleType_{0}", item.ItemIndex), ameeMotorcycleType.SelectedOptions);
                Answer.SetSetting(string.Format("txtMotorcycleDistance_{0}", item.ItemIndex), txtMotorcycleDistance.Text);
                Answer.SetSetting(string.Format("ddlMotorcycleDistance_{0}", item.ItemIndex), ddlMotorcycleDistance.Text);
                var name = string.Format("MotorCycle_{0}", item.ItemIndex);
                var distancePerYear = 0m;
                try
                {
                    distancePerYear = decimal.Parse(txtMotorcycleDistance.Text);
                }
                catch (FormatException) { }

                //if drop down is miles convert mils to km
                if (ddlMotorcycleDistance.SelectedValue == "miles")
                {
                    distancePerYear = distancePerYear * 1.609344m;
                }

                //convert distance per year to distance per month
                var distancePerMonth = distancePerYear / 12m;


                if (ameeMotorcycleType.CurrentCategory.DataItemSelected)
                {
                    var tmpProfileItem = EnsureProfileItem(name, factory, ameeMotorcycleType.CurrentCategory);
                    //if new save
                    bool save = string.IsNullOrEmpty(tmpProfileItem.Id);

                    if (tmpProfileItem.GetValue<decimal>("distanceKmPerMonth") != distancePerMonth)
                    {
                        tmpProfileItem.SetValue("distanceKmPerMonth", distancePerMonth);
                        save = true;
                    }

                    if(save)
                    {
                        factory.Save(tmpProfileItem);
                    }
                    itemsToKeep.Add(Answer.SetItem(name, tmpProfileItem));

                    if (distancePerMonth <= 0m)
                    {
                        completed = false;
                    }
                }
                else
                {
                    itemsToKeep.Add(Answer.SetItem(name, ameeMotorcycleType.CurrentCategory));
                    completed = false;
                }
            }
            else
            {
                completed = false;
            }
        }
        var deleteItems = Answer.AnswerItems.Where(x => !itemsToKeep.Contains(x)).ToList();

        if (deleteItems.Count > 0)
        {
            CarbonExchange.Bll.AnswerItemFactory answerItemFactory = new CarbonExchange.Bll.AnswerItemFactory();
            answerItemFactory.Delete(deleteItems);
            foreach (var del in deleteItems)
            {
                if (Answer.AnswerItems.Contains(del))
                    Answer.AnswerItems.Remove(del);
            }
        }
        return completed;
    }

}
